package 多线程;

/**
 * @Author: 朱胜伟
 * @CreateTime: 2021-02-02 16:46
 * @Description:
 */
public class waitTest {
    public static void main(String[] args) {
        String key1 = "key1";
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (key1){
                    System.out.println("C 代码块开始....");
                    try {
                        key1.wait();  //只能用当前代码块的锁对象调用 wait，用其它的报错
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("C代码块结束");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (key1){
                    System.out.println("B 代码块开始....");
                    try {
                        key1.notify();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("B代码块结束");
                }
            }
        }).start();
    }
}
